package com.carrie.mdg.myviewtext.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.carrie.mdg.myviewtext.R;

import androidLib.activity.BaseActivity;

public class SharePreferencesTestActivity extends BaseActivity {
    private Button saveData, restoreData;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SharePreferencesTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_share_preferences_test);
        saveData = (Button) findViewById(R.id.button_spt_save_data);
        restoreData = (Button) findViewById(R.id.button_restore_data);
    }

    @Override
    protected void initEvent() {
        saveData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                editor.putString("name", "tom");
                editor.putInt("age", 28);
                editor.putBoolean("married", false);
                editor.apply();
            }
        });
        restoreData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
                String name = sp.getString("name", "");
                int age = sp.getInt("age", 10);
                boolean married = sp.getBoolean("married", true);
                Toast.makeText(SharePreferencesTestActivity.this, name + "," + age + "," + married, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void loadData() {

    }
}
